package cn.tedu.wenzhang1.controller;


import cn.tedu.wenzhang1.base.response.JsonResult;
import cn.tedu.wenzhang1.controllerInterface.ArticleIn;
import cn.tedu.wenzhang1.mapper.ArticleMapper;
import cn.tedu.wenzhang1.pojo.dto.ArticleDTO;
import cn.tedu.wenzhang1.pojo.entity.Article;
import cn.tedu.wenzhang1.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("v1/article")
@Api(tags = "文章模块")
public class ArticleController implements ArticleIn {
    @Autowired(required = false)
    private ArticleMapper articleMapper;


    @PostMapping("insert")
    @ApiOperation(value = "发布文章")
    public JsonResult insertArticle(@Validated @RequestBody ArticleDTO articleDTO, @ApiIgnore HttpSession session){
        UserVO userVO=(UserVO) session.getAttribute("user");
        Article article=new Article();
        BeanUtils.copyProperties(articleDTO,article);
        article.setUserId(userVO.getId());
        int i = articleMapper.insertArticle(article);
        if (i == 1)
            return JsonResult.ok();
        return JsonResult.notok();
    }


    @GetMapping("articleID")
    @ApiOperation(value = "文章详情")
    public JsonResult selectById(Long id){
        return JsonResult.ok(articleMapper.selectArticleById(id));
    }



}
